package agency.tango.materialintroscreen.listeners;

import java.io.IOException;

import agency.tango.materialintroscreen.SlideComponentContainer;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.TextTool;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.utils.LongPlainArray;

import agency.tango.materialintroscreen.MessageButtonBehaviour;
import agency.tango.materialintroscreen.adapter.SlidesAdapter;
import agency.tango.materialintroscreen.utils.LogUtil;
import agency.tango.materialintroscreen.ResourceTable;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.listeners
 * @ClassName: MessageButtonBehaviourOnPageSelected
 * @Description: pageslider页面选中之后MessageButton的Behaviour实现
 * @CreateDate: 2021/4/12 16:47
 * @UpdateDate: 2021/4/12 16:47
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */

public class MessageButtonBehaviourOnPageSelected implements IPageSelectedListener {
    private Button messageButton;
    private SlidesAdapter adapter;
    private LongPlainArray<MessageButtonBehaviour> messageButtonBehaviours;

    public MessageButtonBehaviourOnPageSelected(Button messageButton,
                                                SlidesAdapter adapter,
                                                LongPlainArray<MessageButtonBehaviour> messageButtonBehaviours) {
        this.messageButton = messageButton;
        this.adapter = adapter;
        this.messageButtonBehaviours = messageButtonBehaviours;
    }

    @Override
    public void pageSelected(int position) {
        if (adapter == null || position < 0 || position >= adapter.getCount()) {
            return;
        }
        if (messageButton == null || adapter.getItem(position) == null) {
            return;
        }
        final SlideComponentContainer slideComponet = adapter.getItem(position);
        if (slideComponet.hasAnyPermissionsToGrant()) {
            showMessageButton();
            try {
                messageButton.setText(slideComponet.getContext()
                        .getResourceManager().getElement(ResourceTable.String_grant_permissions).getString());
            } catch (IOException | NotExistException | WrongTypeException e) {
                LogUtil.error(this.getClass().getSimpleName(), e.toString());
            }
            messageButton.setClickedListener(component -> slideComponet.askForPermissions());
        } else if (checkIfMessageButtonHasBehaviour(position)) {
            showMessageButton();
            if (messageButtonBehaviours == null ) {
                return;
            }
            String messageButtonText =
                    messageButtonBehaviours.get(position).isPresent() ?
                            messageButtonBehaviours.get(position).get().getMessageButtonText() : "";
            messageButton.setText(messageButtonText);
            Component.ClickedListener clickedListener =
                    messageButtonBehaviours.get(position).isPresent() ?
                            messageButtonBehaviours.get(position).get().getClickListener() : null;
            messageButton.setClickedListener(clickedListener);
        } else if (messageButton.getVisibility() != Component.INVISIBLE) {
            AnimatorProperty animator = messageButton.createAnimatorProperty();
            if (animator == null) {
                animator = new AnimatorProperty(messageButton);
            }
            animator.setDuration(200).alphaFrom(1).alpha(0)
                    .setCurveType(Animator.CurveType.ACCELERATE).setTarget(messageButton);
            animator.start();
            messageButton.setVisibility(Component.INVISIBLE);
        } else {
            LogUtil.info("MessageButtonBehaviourOnPageSelected", "do nothing");
        }
    }

    /**
     * 检查MessageButton是否存在Behaviour
     *
     * @param position MessageButton在pageslider里面所处页面的index
     * @return 是否存在Behaviour
     */
    private boolean checkIfMessageButtonHasBehaviour(int position) {
        if (messageButtonBehaviours == null  || !messageButtonBehaviours.get(position).isPresent()) {
            return false;
        }
        return messageButtonBehaviours.get(position).get() != null
                && !TextTool.isNullOrEmpty(messageButtonBehaviours.get(position).get().getMessageButtonText());
    }

    /**
     * 显示MessageButton
     */
    private void showMessageButton() {
        if (messageButton.getVisibility() != Component.VISIBLE) {
            messageButton.setVisibility(Component.VISIBLE);
            AnimatorProperty animator = new AnimatorProperty(messageButton);
            animator.setDuration(200).alphaFrom(0).alpha(1).setCurveType(Animator.CurveType.DECELERATE);
            animator.start();
        }
    }
}